import request from '@/utils/request'

const getFormData = (params) => {
  const filtersRaw = []
  for (const property in params) {
    let value = params[property]
    if (property === 'categoryName' && value === '软床') {
      value = '床'
    }
    if (![null, undefined, ''].includes(value)) {
      filtersRaw.push({ id: `${property}0`, property, operator: '=', value })
    }
  }
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return data
}

/**
 * 销量总体分析
 * @param {*} params
 * @returns
 */
export function getOverAllSalesReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/overAllSalesReport',
    method: 'post',
    data
  })
}

/**
 * 区域销量分析
 * @param {*} params
 * @returns
 */
export function getAreaSalesReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/areaSalesReport',
    method: 'post',
    data
  })
}

/**
 * 区域销量结构分析
 * @param {*} params
 * @returns
 */
export function queryAreaSalesStructureReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/areaSalesStructureReport',
    method: 'post',
    data
  })
}

/**
 * 产品专区分析
 * @param {Object} params
 * @returns
 */
export function queryProductZoneReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/productZoneReport',
    method: 'post',
    data
  })
}

/**
 * 市场层级分析
 * @param {Object} params
 * @returns
 */
export function queryMarketLevelReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/marketLevelReport',
    method: 'post',
    data
  })
}

/**
 * 价格分区品类查询
 * @param {Object} params
 * @returns
 */
export function queryAreaCategory(params) {
  const data = new FormData()
  data.append('pageFlag', false)
  return request({
    url: '/api/v1/displayAreaCategory/page',
    method: 'post',
    data
  })
}
/**
 * 价格分区分析
 * @param {Object} params
 * @returns
 */
export function queryPriceZoneReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/priceZoneReport',
    method: 'post',
    data
  })
}
/**
 * 规格分析
 * @param {Object} params
 * @returns
 */
export function querySpecificationsReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/specificationsReport',
    method: 'post',
    data
  })
}

/**
 * 尺寸分析报表
 * @param {Object} params
 * @returns
 */
export function querySizeReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/sizeReport',
    method: 'post',
    data
  })
}

/**
 * 风格分析报表
 * @param {Object} params
 * @returns
 */
export function queryStyleReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/styleReport',
    method: 'post',
    data
  })
}

/**
 * 上市时间分析报表
 * @param {Object} params
 * @returns
 */
export function queryMarketTimeReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/marketTimeReport',
    method: 'post',
    data
  })
}

/**
 * 产品TOP排名分析报表
 * @param {Object} params
 * @returns
 */
export function queryTopReport(params) {
  const data = getFormData(params)
  return request({
    url: '/api/v1/displaySoftSaleNum/topReport',
    method: 'post',
    data
  })
}

/**
 * 根据产品获取材质类型（产品TOP排名分析报表）
 * @param {Object} params
 * @returns
 */
export const getBySuperCode = async(code) => {
  const res = await request({
    url: '/api/v1/displayAreaCategory/getBySuperCode/' + code,
    method: 'get'
  })
  return res && res.data || []
}

/**
 * 获取规格列表（产品TOP排名分析报表）
 * @param {Object} params
 * @returns
 */
export const getSaleStyleName = async(data) => {
  const res = await request({
    url: '/api/v1/displaySoftSaleNum/getSaleStyleName',
    method: 'post',
    data
  })
  return res && res.data || []
}

/**
 * 导出
 * @param {object} params 查询参数
 * @param {*} url 请求地址
 * @returns
 */
export function queryDownloadData(params, url) {
  const data = getFormData(params)
  return request({
    url,
    method: 'post',
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
